/**
 * @param {string} digits
 * @return {string[]}
 */
var letterCombinations = function (digits) {
  if (!digits) return []
  //定义数组res
  let res = []
  //定义映射关系：电话号码 -> 字符串
  const phoneMap = {
    2: 'abc',
    3: 'def',
    4: 'ghi',
    5: 'jkl',
    6: 'mno',
    7: 'pqrs',
    8: 'tuv',
    9: 'wxyz'
  };

  // 定义一个回溯函数
  function backTrack(index, path) {
    if (path.length === digits.length) {
      res.push(path)
      return
    }
    const letters = phoneMap[digits[index]]
    for (let char of letters) {
      path += char
      backTrack(index + 1, path)
      path = path.slice(0, path.length - 1)
    }
  }

  //调用回溯函数
  backTrack(0, '')

  //返回数组res
  return res
};